<template>
  <div class="container">
    <div class="data_box" style="height: 540px;">

      <!-- 搜索框 -->
      <MySearch @parent-searchBtn="searchBtn" ></MySearch>

      <div class="add" style="width:100px;height:40px; float: right; margin-bottom: -10px;">
        <el-button size="mini" type="primary" @click="handleAdd()" style="width: 80px">
          新增
        </el-button>
      </div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="交易日期" >
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.traDate | dateFormat }}</span>
          </template>
        </el-table-column>
        <el-table-column label="订单 ID" >
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <p>用户ID: {{ scope.row.userId }}</p>
              <p>宠物粮ID: {{ scope.row.foodId }}</p>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ scope.row.orderId }}</el-tag>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="总价" >
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <p>数量: {{ scope.row.count }}</p>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ scope.row.total }}</el-tag>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页按钮 -->
    <div class="pagination-box">
      <my-pagination
        style="text-align: right; margin-right: 100px"
        @parent-getData="getData"
        :dataCount="dataCount"
      ></my-pagination>
    </div>

    <!-- 编辑弹出框 -->
    <div class="container" style="display: inline-block">
      <!-- Form -->
      <el-dialog :title="editBoxTitle" :visible.sync="dialogFormVisible">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="用户 ID" label-width="68px">
            <el-input v-model="formInline.userId"></el-input>
          </el-form-item>
          <el-form-item label="宠物粮ID">
            <el-input v-model="formInline.foodId"></el-input>
          </el-form-item>
          <el-form-item label="总价" label-width="68px">
            <el-input v-model="formInline.total"></el-input>
          </el-form-item>
          <el-form-item label="数量" label-width="68px">
            <el-input v-model="formInline.count"></el-input>
          </el-form-item>
          <el-form-item label="交易时间">
            <div class="block">
              <el-date-picker
                v-model="formInline.traDate"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </div>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="handleCancelBtn()">取 消</el-button>
          <el-button type="primary" @click="handleConfirmBtn()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 编辑弹出框多的end -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      pageNum: 1,
      dataCount: 0,  // 获取数据总条数
      tableData: [],
      dialogFormVisible: false, //编辑框弹出标志
      formInline: {   // 编辑框数据绑定
        orderId: '',
        userId: '',
        foodId: '',
        total: '',
        count: '',
        traDate: ''
      },
      editBoxTitle: '修改订单信息'  // 编辑框的标题
    }
  },
  mounted() {
    this.getData(this.pageNum, 5);
    this.getDataCount();
  },
  methods: {
    handleAdd() {
      this.editBoxTitle = '添加订单信息'
      this.dialogFormVisible = true
      this.formInline = {}
    },
    handleEdit(data) {
      this.editBoxTitle = '修改订单信息'
      this.dialogFormVisible = true;
      this.initFormInline(data);
    },
    handleDelete(index, itemData) {
      this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(()=>{
        // 执行删除
        this.onDelData(itemData.orderId);
      }).then(() => {
        this.tableData.splice(index, 1)
        this.getData(this.pageNum, 5)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    handleCancelBtn(){
      this.dialogFormVisible = false
      this.formInline = {}
    },
    handleConfirmBtn(){
      if(this.editBoxTitle == '修改订单信息'){
        this.onUpdate()
      } else {
        this.onAddData()
      }
    },
    // 根据页码发起请求获取数据
    getData(pageNum, pageSize) {
      // 分页组件调用此方法时，更新当前页码
      this.pageNum = pageNum;
      this.$axios
        .get(`order/getByPage?pageNum=${pageNum}&pageSize=${pageSize}`)
        .then((result) => {
          this.tableData = result.data;
        })
        .catch((err) => {
          alert(err);
        });
    },
    // 获取数据的总条数
    getDataCount() {
      this.$axios
        .get("order/getDataCount")
        .then((result) => {
          this.dataCount = result.data;
        })
        .catch((err) => {
          alert(err);
        });
    },
    onAddData(){
      this.$axios.post("order/insertOrder", {
        userId: this.formInline.userId,
        foodId: this.formInline.foodId,
        total: this.formInline.total,
        count: this.formInline.count,
        traDate: this.formInline.traDate
      }).then((result) => {
        if (result.data == 1) {
            this.dialogFormVisible = false;
            // 添加数据后刷新数据
            this.getData(this.pageNum, 5);
            this.getDataCount()
            this.$message({
              showClose: true,
              message: "添加成功！",
              type: "success",
            });
        }
        this.formInline = {}
      }).catch((err) => {
        alert(err)
      });
    },
    onDelData(orderId){
      this.$axios.get("order/deleteOrderById?orderId=" + orderId)
      .then((result) => {
        if(result.data == 1){
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        } else {
          this.$message({
            showClose: true,
            message: '删除失败',
            type: 'warning'
          });
        }
      }).catch((err) => {
        this.$message({
            showClose: true,
            message: '错误信息：' + err,
            type: 'warning'
        });
      });
    },
    onUpdate(){
      this.$axios
        .post("order/updateOrderById", {
          orderId: this.formInline.orderId,
          userId: this.formInline.userId,
          foodId: this.formInline.foodId,
          total: this.formInline.total,
          count: this.formInline.count,
          traDate: this.formInline.traDate
        })
        .then((result) => {
          if (result.data == 1) {
            this.dialogFormVisible = false;
            // 编辑数据后刷新数据
            this.getData(this.pageNum, 5);
          }
        })
        .then(() => {
          this.$message({
            showClose: true,
            message: "更新成功！",
            type: "success",
          });
        })
        .catch((err) => {
          alert(err);
        });
    },
    initFormInline(data){
      let newForm = {
        orderId: data.orderId,
        userId: data.userId,
        foodId: data.foodId,
        total: data.total,
        count: data.count,
        traDate: data.traDate
      };
      this.formInline = newForm;
    },
    searchBtn(value) {
            if (value != "") {
                let newTable = this.tableData.filter(item => item.orderId == value);
                this.tableData = newTable;
            }
            else {
                this.getData(this.pageNum, 5);
            }
        }

  },
}
</script>
<style>
.demo-form-inline {
  padding: 0px 20px;
}
.el-form-item .el-input {
  width: 250px;
}
</style>